/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.searchbook.keywordsflow;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.KeyEvent.DispatcherState;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class QueryTextView extends EditText {
	private static final boolean DBG = true;
	private static final String TAG = "QSB.QueryTextView";
	private InputMethodManager imm;
	private Activity mContext;

	public QueryTextView(Context paramContext) {
		super(paramContext);
	}

	public QueryTextView(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
	}

	public QueryTextView(Context paramContext, AttributeSet paramAttributeSet,
			int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
	}

	private Activity getActivity() {
		if ((this.mContext != null) && (this.mContext instanceof Activity))
			return this.mContext;
		return null;
	}

	public boolean dispatchKeyEventPreIme(KeyEvent paramKeyEvent) {
		Activity localActivity = getActivity();
		if ((localActivity != null) && (paramKeyEvent.getKeyCode() == 4)) {
			KeyEvent.DispatcherState localDispatcherState = getKeyDispatcherState();
			if (localDispatcherState != null) {
				if ((paramKeyEvent.getAction() == 0)
						&& (paramKeyEvent.getRepeatCount() == 0)) {
					localDispatcherState.startTracking(paramKeyEvent, this);
					return false;
				}
				if ((paramKeyEvent.getAction() == 1)
						&& (!(paramKeyEvent.isCanceled()))
						&& (localDispatcherState.isTracking(paramKeyEvent))) {
					hideInputMethod();
					localActivity.onBackPressed();
					return false;
				}
			}
		}
		return super.dispatchKeyEventPreIme(paramKeyEvent);
	}

	public InputMethodManager getInputMethodManager() {
		return ((InputMethodManager) getContext().getSystemService(
				"input_method"));
	}

	public void hideInputMethod() {
		InputMethodManager localInputMethodManager = getInputMethodManager();
		if ((localInputMethodManager == null)
				|| (!(localInputMethodManager.isActive())))
			return;
		localInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
	}

	public void onCommitCompletion(CompletionInfo paramCompletionInfo) {
		hideInputMethod();
		replaceText(paramCompletionInfo.getText());
	}

	protected void replaceText(CharSequence paramCharSequence) {
		clearComposingText();
		setText(paramCharSequence);
		setTextSelection(false);
	}

	public void setActivity(Activity paramActivity) {
		this.mContext = paramActivity;
	}

	public void setTextSelection(boolean paramBoolean) {
		if (paramBoolean) {
			selectAll();
			return;
		}
		setSelection(length());
	}

	public void showInputMethod() {
		InputMethodManager localInputMethodManager = getInputMethodManager();
		if (localInputMethodManager == null)
			return;
		localInputMethodManager.showSoftInput(this, 0);
	}
}